package com.qdlc.p2p.biz.borrowworker;

import com.qdlc.p2p.biz.borrowworker.worker.BaseBorrowWorker;
import com.qdlc.p2p.biz.borrowworker.worker.BorrowWorker;
import com.qdlc.p2p.biz.borrowworker.worker.CreditBorrowWorker;
import com.qdlc.p2p.biz.borrowworker.worker.DeliverGoodsBorrowWorker;
import com.qdlc.p2p.biz.borrowworker.worker.MortgageBorrowWorker;
import com.qdlc.p2p.biz.borrowworker.worker.OffVouchBorrowWorker;
import com.qdlc.p2p.biz.borrowworker.worker.SecondBorrowWorker;
import com.qdlc.p2p.common.constant.BorrowStatus;
import com.qdlc.p2p.common.constant.BorrowType;
import com.qdlc.p2p.common.util.BeanUtil;
import com.qdlc.p2p.dal.dao.BorrowConfigDao;
import com.qdlc.p2p.dal.dto.Borrow;
import com.qdlc.p2p.dal.dto.BorrowConfig;

/**
 * 生成标工厂类
 * 
 * @author：lhm
 * @version 2.0
 * @since 2014年7月11日
 */
public final class BorrowHelper {
	/**
	 * 构造函数私有化
	 */
	private BorrowHelper() {

	}

	/**
	 * 根据标属性，生出对应的标Worker对象
	 * 
	 * @param borrow 标对象
	 * @return BorrowWorker
	 */
	public static BorrowWorker getWorker(Borrow borrow) {
		BorrowConfigDao borrowConfigDao = (BorrowConfigDao) BeanUtil.getBean("borrowConfigDao");
		BorrowConfig cfg = borrowConfigDao.find(new Long(borrow.getType()));
		boolean flag = false; // 判断是否是发标或复审或投标
		switch (borrow.getType()) {
		case 100:
			return new BaseBorrowWorker(borrow, cfg);
		case BorrowType.TYPE_SECOND:
			borrow.setTimeLimit(1);
			return new SecondBorrowWorker(borrow, cfg);
		case BorrowType.TYPE_CREDIT:
			// 发标或复审或投标
            flag = (borrow.getStatus() == BorrowStatus.STATUS_PUBLISHING) || (borrow.getStatus() == BorrowStatus.STATUS_RECHECK_PASS) || (borrow.getIsTender() == 1);
			return new CreditBorrowWorker(borrow, cfg, flag);
		case BorrowType.TYPE_MORTGAGE:
			// 发标或复审或投标
			if ((borrow.getStatus() == BorrowStatus.STATUS_PUBLISHING) || (borrow.getStatus() == BorrowStatus.STATUS_RECHECK_PASS) || (borrow.getIsTender() == 1)) {
				flag = true;
			}
			return new MortgageBorrowWorker(borrow, cfg, flag);
		case BorrowType.TYPE_OFFVOUCH:
			// 发标或复审或投标
			if ((borrow.getStatus() == BorrowStatus.STATUS_PUBLISHING) || (borrow.getStatus() == BorrowStatus.STATUS_RECHECK_PASS) || (borrow.getIsTender() == 1)) {
				flag = true;
			}
			return new OffVouchBorrowWorker(borrow, cfg, flag);
		case BorrowType.DELIVER_GOODS:
			// 发标或复审或投标
			if ((borrow.getStatus() == BorrowStatus.STATUS_PUBLISHING) || (borrow.getStatus() == BorrowStatus.STATUS_RECHECK_PASS) || (borrow.getIsTender() == 1)) {
				flag = true;
			}
			return new DeliverGoodsBorrowWorker(borrow, cfg, flag);
		default:
			throw new RuntimeException("不正确的Borrow类型:" + borrow.getType());
		}
	}

}
